/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.Modem;
import br.com.jr2tecnologia.model.persistence.DAOException;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.ModemRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class ModemPersistence implements ModemPersistenceLocal{

    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private ModemRepository modemRepository;

    @PostConstruct
    public void initialize(){
        modemRepository = DAOFactory.getModemRepository(em);
    }

    public void persist(Modem modem){
        modemRepository.persist(modem);
    }

    public void update(Modem modem){
        modemRepository.update(modem);
    }

    public void remove(Modem modem){
        modemRepository.remove(modem, modem.getPkModem());
    }

    public Modem findById(Modem modem){
        Modem entity = null;
        try{
            entity = modemRepository.findById(modem.getPkModem());
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot find login for this name: " + modem.getPkModem(), e);
        }
        return entity;
    }

    public Modem findModemByName(String name) throws PersistenceUnitException{
        Modem entity = null;
        try{
            entity = modemRepository.findByName(name);
        }catch(DAOException ex){
            logger.severe("cannot find modem for this name: " + name);
            throw new PersistenceUnitException(ex);
        }
        return entity;
    }

    public Modem findModemBySmscRoute(String name) throws PersistenceUnitException{
        Modem entity = null;
        try{
            entity = modemRepository.findBySmscRoute(name);
        }catch(DAOException ex){
            logger.severe("cannot find modem for this name: " + name);
            throw new PersistenceUnitException(ex);
        }
        return entity;
    }

    public List<Modem> list(Modem modem){
        try{
            return modemRepository.list(modem);
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list modem", e);
            throw new DAOException(e);
        }
    }

    public List<Modem> listAllSharedModems(){
        try{
            return modemRepository.listAllSharedModems();
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list modem", e);
            throw new DAOException(e);
        }
    }

    public List<Modem> listAll(){
        try{
            return modemRepository.listAll();
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list modem", e);
            throw new DAOException(e);
        }
    }
}
